package com.chen.controller;

import com.chen.User;

import com.chen.exception.UserException;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/14.
 */
//@Controller
//@RequestMapping("/user")
public class UserController {
//
//    private Map<String,User>users=new HashMap<String ,User>();
//    public UserController() {
//        users.put("sdy", new User("sdy", "3234", "宋冬野", "123"));
//        users.put("mmm", new User("mmm", "3234", "密码吗", "123"));
//        users.put("lili", new User("lili", "3234", "丽丽", "123"));
//        users.put("jvb", new User("jvb", "3234", "举报", "123"));
//        users.put("scff", new User("scff", "3234", "生菜夫妇", "123"));
//
//
//    }
//    @RequestMapping(value = "/users",method = RequestMethod.GET)
//    public String list(Model model) {
//        model.addAttribute("users", users);
//
//        return "user/list";
//
//    }
//
////    链接到add页面时是get请求，会访问这段代码
//    @RequestMapping(value = "/add", method = RequestMethod.GET)
//    public String add(Model model){
////    new 新用户    开启modelDriven
//
//        model.addAttribute(new User());
//        return "user/add";
//
//    }
////    在具体添加用户时是post请求，就访问下面代码，文件添加
//    @RequestMapping(value = "/add", method = RequestMethod.POST)
//    public String add(@Validated User user,BindingResult br,@RequestParam("attachs")MultipartFile[] attachs,HttpServletRequest req) throws IOException {
//        if (br.hasErrors()){
//            return "user/add";
//        }
//        String realpath=req.getSession().getServletContext().getRealPath("/resources/upload");
//        System.out.println(realpath);
//        for (MultipartFile attach:attachs) {
//            if (attach.isEmpty())continue;
//            File f = new File(realpath + "/" + attach.getOriginalFilename());
//            FileUtils.copyInputStreamToFile(attach.getInputStream(), f);
//            System.out.println(attach.getName() + "," + attach.getOriginalFilename() + "," + attach.getContentType());
//        }
//        users.put(user.getUsername(), user);
//        return "redirect:/user/users";//客户端跳转
//
//
//    }
//
////    查看详情页面
//        @RequestMapping(value = "/{username}",method = RequestMethod.GET)
//        public String show(@PathVariable String username,Model model){
//            model.addAttribute(users.get(username));
//            return "user/show";
//        }
//
//        //转化成json数据
//    @RequestMapping(value = "/{username}",method = RequestMethod.GET,params = "json")
//    @ResponseBody
//    public User show(@PathVariable String username){
//        return users.get(username);
//    }
//
//
//
////修改用户信息
//    @RequestMapping(value = "/{username}/update",method = RequestMethod.GET)
//    public String update(@PathVariable String username,Model model){
//        model.addAttribute(users.get(username));
//        return "user/update";
//    }
////    修改信息
//    @RequestMapping(value ="/{username}/update",method = RequestMethod.POST)
//    public String update(@PathVariable String username,@Validated User user,BindingResult br){
//        if (br.hasErrors()){
//            return "user/update";
//        }
//        users.put(username, user);
//        return "redirect:/user/users";
//    }
////删除信息
//    @RequestMapping(value = "/{username}/delete",method =RequestMethod.GET)
//    public String delete(@PathVariable String username){
//        users.remove(username);
//        return "redirect:/user/users";
//    }
////    登录页面
//    @RequestMapping(value = "/login",method = RequestMethod.GET)
//    public String login(Model model) {
//        model.addAttribute("users", users);
//
//        return "user/login";
//
//    }
////登录和验证
//    @RequestMapping(value = "/login",method = RequestMethod.POST)
//    public String login(String username,String password,HttpSession session){
//        if (!users.containsKey(username)){
//            throw new UserException("用户名不存在");
//        }
//        User u=users.get(username);
//        if (!u.getPassword().equals(password)){
//            throw new UserException("密码错误");
//
//        }
//        session.setAttribute("loginUser", u);
//        return "redirect:/user/users";
//    }
////    局部异常处理
//    @ExceptionHandler(value = {UserException.class})
//    public String handlerException(UserException e,HttpServletRequest req){
//        req.setAttribute("e",e);
//        return "user/error";
//
//    }
//

}
